package com.design.fitment.adapter;

/**
 * Created by 娇哥 on 2016/1/15.
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.design.fitment.R;
import com.design.fitment.app.MyApplication;
import com.design.fitment.entity.CasesDtail;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

    public class CaseDetailListAdapter extends BaseAdapter {

        private List<CasesDtail.DataEntity.ImgListEntity> objects = new ArrayList<>();
        private ImageLoader imageLoader;
        private DisplayImageOptions options;
        private Context context;
        private LayoutInflater layoutInflater;

        public CaseDetailListAdapter(Context context,List<CasesDtail.DataEntity.ImgListEntity> objects) {
            this.context = context;
            this.layoutInflater = LayoutInflater.from(context);
            imageLoader = MyApplication.getApp().getImageLoader();
            options = MyApplication.getApp().getImageOptions();
            this.objects = objects;
        }

        @Override
        public int getCount() {
            return objects.size();
        }

        @Override
        public CasesDtail.DataEntity.ImgListEntity getItem(int position) {
            return objects.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.cases_detail_list_item, null);
                viewHolder = new ViewHolder();
                viewHolder.ivCasesDetailItem = (ImageView) convertView.findViewById(R.id.iv_cases_detail_item);
                viewHolder.tvListItem = (TextView) convertView.findViewById(R.id.tv_list_item);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();

            }
            imageLoader.displayImage(objects.get(position).getImgUrl(), viewHolder.ivCasesDetailItem, options);
            if (objects.get(position).getName()!=null){
                viewHolder.tvListItem.setVisibility(View.VISIBLE);
                viewHolder.tvListItem.setText(objects.get(position).getName());
            }else{
                viewHolder.tvListItem.setVisibility(View.GONE);
            }

            return convertView;
        }



        protected class ViewHolder {
            private ImageView ivCasesDetailItem;
            private TextView tvListItem;
        }
    }


